package com.example.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

/**
 * Created by lianlei on 2017/1/13.
 */
public class MyMd5 {

    /**
     * BASE64解密
     * @param key
     * @return
     * @throws Exception
     */
    public static String decryptBASE64(String key){
        if ( key == null || key.equals ("") ){
            return "";
        }
        String pwd = null;
        try {
            pwd = new String ((new BASE64Decoder ()).decodeBuffer(key));
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return pwd;
    }

    /**
     * BASE64加密
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(String key) {
        if ( key == null || key.equals ("") ){
            return "";
        }
        return (new BASE64Encoder ()).encodeBuffer(key.getBytes());
    }

    public static void main(String[] args) throws Exception
    {
        String data = MyMd5.encryptBASE64("lianlei");
        System.out.println("加密前："+data);

        System.out.println (data.length ());

        String byteArray = MyMd5.decryptBASE64(data);
        System.out.println("解密后："+new String(byteArray));
    }
}
